const { parallelTaskLimiter } = require("../util/parallelTaskLimiter");

async function task() {
  return new Promise((resolve, reject) => {
    const time = Math.floor(Math.random() * 1000) + 1000;
    setTimeout(() => {
      if (time > 1500) {
        reject(time);
      } else {
        resolve(time);
      }
    }, 1000);
  })
    .then((time) => {
      console.log("rs", time);
      return true;
    })
    .catch((time) => {
      console.log("rj", time);
      return false;
    });
}

async function example() {
  const list = [];

  let count = 10;
  while (count--) {
    list.push(task);
  }

  console.time("finished");
  parallelTaskLimiter(list, 2).then((result) => {
    console.log(result);
    console.timeEnd("finished");
  });
}

example();
